package org.ruoyi.aibox.service.impl;

import org.ruoyi.common.core.utils.MapstructUtils;
import org.ruoyi.core.page.TableDataInfo;
import org.ruoyi.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.ruoyi.aibox.domain.bo.DocAuditItemBo;
import org.ruoyi.aibox.domain.vo.DocAuditItemVo;
import org.ruoyi.aibox.domain.DocAuditItem;
import org.ruoyi.aibox.mapper.DocAuditItemMapper;
import org.ruoyi.aibox.service.IDocAuditItemService;
import org.ruoyi.common.core.utils.StringUtils;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 文档审核项Service业务层处理
 *
 * @author xpeter
 * @date 2025-04-28
 */
@RequiredArgsConstructor
@Service
public class DocAuditItemServiceImpl implements IDocAuditItemService {

    private final DocAuditItemMapper baseMapper;

    /**
     * 查询文档审核项
     */
    @Override
    public DocAuditItemVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询文档审核项列表
     */
    @Override
    public TableDataInfo<DocAuditItemVo> queryPageList(DocAuditItemBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<DocAuditItem> lqw = buildQueryWrapper(bo);
        Page<DocAuditItemVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询文档审核项列表
     */
    @Override
    public List<DocAuditItemVo> queryList(DocAuditItemBo bo) {
        LambdaQueryWrapper<DocAuditItem> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<DocAuditItem> buildQueryWrapper(DocAuditItemBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<DocAuditItem> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getAudititemName()), DocAuditItem::getAudititemName, bo.getAudititemName());
        lqw.eq(StringUtils.isNotBlank(bo.getAuditObject()), DocAuditItem::getAuditObject, bo.getAuditObject());
        lqw.eq(StringUtils.isNotBlank(bo.getAuditChapter()), DocAuditItem::getAuditChapter, bo.getAuditChapter());
        lqw.eq(StringUtils.isNotBlank(bo.getAudititemContent()), DocAuditItem::getAudititemContent, bo.getAudititemContent());
        lqw.eq(StringUtils.isNotBlank(bo.getAudititemPrompt()), DocAuditItem::getAudititemPrompt, bo.getAudititemPrompt());
        lqw.eq(StringUtils.isNotBlank(bo.getAuditType()), DocAuditItem::getAuditType, bo.getAuditType());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), DocAuditItem::getStatus, bo.getStatus());
        lqw.like(bo.getCreateBy() != null, DocAuditItem::getCreateBy, bo.getCreateBy());
        lqw.eq(bo.getCreateDept() != null, DocAuditItem::getCreateDept, bo.getCreateDept());
        return lqw;
    }

    /**
     * 新增文档审核项
     */
    @Override
    public Boolean insertByBo(DocAuditItemBo bo) {
        DocAuditItem add = MapstructUtils.convert(bo, DocAuditItem.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改文档审核项
     */
    @Override
    public Boolean updateByBo(DocAuditItemBo bo) {
        DocAuditItem update = MapstructUtils.convert(bo, DocAuditItem.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(DocAuditItem entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除文档审核项
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
